/**
 * 
 */
package com.example.radioclub.music.helper;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;

import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.SimpleClientHttpRequestFactory;

import android.util.Log;

import com.example.radioclub.data.Constants;
import com.example.radioclub.music.tasks.RestClientAuthDownloader;

/**
 * This class defines request which will be used to create RestTemplate instance with
 * certain property.
 * 
 * @author wizard
 *
 */
public class ClientRequestFactory extends SimpleClientHttpRequestFactory {
	private final String TAG = Constants.APP_TAG + "Client Request Factory";
	private String headerValue = null, headerName = null;
	private String[] headerValues = null, headerNames = null;

	public ClientRequestFactory(String headerName, String headerValue) {
		this.headerName = headerName;
		this.headerValue = headerValue;
		Log.i(TAG, this.headerName + " : " + this.headerValue);
	}
	
	public ClientRequestFactory(String[] headerNames, String[] headerValues) {
		this.headerValues = headerValues;
		this.headerNames = headerNames;
	}

	@Override
	public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod)
			throws IOException {
		// TODO Auto-generated method stub
		return super.createRequest(uri, httpMethod);
	}

	@Override
	protected void prepareConnection(HttpURLConnection connection,
			String httpMethod) throws IOException {
		// TODO Auto-generated method stub
		int size = 0;
		if(this.headerValues != null && this.headerNames != null) {
			if(this.headerValues.length == this.headerNames.length) {
				size = this.headerValues.length;
			}			
			for(int i=0; i<size; ++i) {
				connection.setRequestProperty(headerNames[i], headerValues[i]);
			}
		}
		if(this.headerName != null && this.headerValue != null) {
			connection.setRequestProperty(headerName, headerValue);			
		}
		super.prepareConnection(connection, httpMethod);
	}

}
